﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zor_IDAL;
using Zor_Modes;
using Zor_Tools;
namespace Zor_DAL
{
  public   class Sys_Users_DAL<T> where T : class
    {
        #region -用户管理-  
        /// <summary>
        /// 根据用户Id查询数据
        /// create zhl by 17-3-26
        /// </summary>
        /// <returns></returns>
        ///      
          public static List<T> SelectDepartmentTree()
          {
              StringBuilder sb = new StringBuilder();
              sb.Append("WITH cte AS (select Dt_Id,Dt_Name,Dt_ParentId,0 as Lev,cast(Dt_Id as varbinary) as Dt_Sort ");
              sb.Append("from Sys_Department where Dt_ParentId=0 UNION ALL SELECT dt.Dt_Id,dt.Dt_Name,dt.Dt_ParentId,Dt_Level+1,cast(c.Dt_Sort+cast(dt.Dt_Id as varbinary) as varbinary) as Dt_Sort  ");
              sb.Append("from cte as c JOIN Sys_Department as dt on dt.Dt_ParentId=c.Dt_Id)select *,REPLACE((SPACE(Lev *2)+ Dt_Name),' ','.') AS LevName from cte order by Dt_Sort  ");
              return DapperQuery<T>.DoQueryList(sb.ToString(), null);

          }

        /// <summary>
        /// 根据用户Id查询数据
        /// create zhl by 17-3-17
        /// </summary>
        /// <returns></returns>
        ///      
        public static T SelectPageListId(string user_Id)
        {
            var sql = "select * from [Sys_Users] where User_Id=@user_Id";
            return DapperQuery<T>.DoQueryT(sql, new { User_Id = user_Id });
        }
        /// <summary>
        /// 查询所有权限Id和Name 
        /// create zhl by 17-3-17
        /// </summary>
        /// <returns></returns>
        public static List<T> SelectRols()
        {
            var sql = "SELECT Roles_Id	,Role_Name	FROM  dbo.Sys_Roles";         
            return DapperQuery<T>.DoQueryList(sql,null);
        }    
       
        /// <summary>
        /// 用户管理-新增
        /// create zhl by 17-3-17
        /// </summary>
        /// <param name="sysuser">用户对象</param>
        /// <returns>执行结果：1 成功 0失败</returns>
        public static BaseAnswer Insertsysusers(Sys_Users sysuser)
        {
            sysuser.IsDeleted = 1;
            var sql = @"if not exists(select User_UserName from [Sys_Users] where User_UserName  =@User_UserName ) 
                        insert into Sys_Users values 
                        (@Roles_Id,@DT_Id,@User_UserName,@User_Password,@User_RealName,@User_Phone,@Is_Admin,@Is_Available,@User_CreateDate,@User_CreateName,@User_Remarks,@IsDeleted,@User_ErrorNum)";
            BaseAnswer bar = DapperInsert<T>.DoInsert(sql, sysuser);
            return bar;
          
        }

        /// <summary>
        /// 用户管理-修改
        /// create zhl by 17-3-17
        /// </summary>
        /// <param name="sysuser">用户对象</param>
        /// <returns>执行结果：1 成功 0失败</returns>
        public static BaseAnswer Updatesysusers(Sys_Users sysuser)
        {
            sysuser.IsDeleted = 1;
            var sql = "";
            if (sysuser.User_UserName != null)
                sql += "if  exists(select User_UserName from [Sys_Users] where User_UserName =@User_UserName ) ";
                sql += "update [Sys_Users] set User_UserName=@User_UserName,Roles_Id=@Roles_Id,DT_Id=@DT_Id,User_RealName=@User_RealName,User_Phone=@User_Phone,Is_Admin=@Is_Admin,Is_Available=@Is_Available  where User_Id = @User_Id";
            BaseAnswer bar = DapperEdit<T>.DoEdit(sql, new Sys_Users { User_UserName = sysuser.User_UserName, Roles_Id = sysuser.Roles_Id, DT_Id = sysuser.DT_Id, User_RealName = sysuser.User_RealName, User_Phone = sysuser.User_Phone, Is_Admin = sysuser.Is_Admin, Is_Available = sysuser.Is_Available, User_Id = sysuser.User_Id });
            return bar;          
        }
        /// <summary>
        /// 删除用户
        /// create zhl by 17-3-17
        /// </summary>
        /// <param name="id">删除用户的id</param>
        /// <returns>1正常 返回0</returns>
        public static BaseAnswer Deletesysusers(int[] id)
        {

            var sql = @"update Sys_Users set IsDeleted=2 where User_Id=@User_Id";
            BaseAnswer bar = DapperDelete<T>.DoDelete(sql, new { User_Id = id.ToArray<int>() });
            return bar;          
        }

        /// <summary>
        /// 设置账户为可用
        /// <param name="id">修改用户的id</param>
        /// </summary>
        /// <returns></returns>
        public static BaseAnswer UnlockUser(int[] id)
        {
            var sql = "update [Sys_Users] set  User_ErrorNum =@User_ErrorNum,Is_Available=@Is_Available where User_Id = @User_Id";          
            BaseAnswer bar = DapperEdit<T>.DoEdit(sql, new  { User_ErrorNum = 0, Is_Available = 0, User_Id = id.ToArray<int>() });
            return bar;            
        }
        #endregion
    }
}
